#ifndef RETURNBOOK_H
#define RETURNBOOK_H

//图书归还界面类
#include <QWidget>
#include <QList>
#include <QFile>
#include "borrowedbook.h"
#include <QCheckBox>
#include <QKeyEvent>
#include <QShortcut>

QT_BEGIN_NAMESPACE
namespace Ui {
class ReturnBook;
}
QT_END_NAMESPACE
class ReturnBook : public QWidget
{
    Q_OBJECT
public:
    explicit ReturnBook(QWidget *parent = nullptr);
    ~ReturnBook();
    void addDataToHistory(QList<BorrowedBook*>book);
    void deleteDataFromBorrowed(QList<BorrowedBook *> books);
    QList<BorrowedBook *> getBorroweredBooks() const;
    void setBorroweredBooks(const QList<BorrowedBook *> &newBorroweredBooks);

    QList<BorrowedBook *> getCurrentShowData() const;
    void setCurrentShowData(const QList<BorrowedBook *> &newCurrentShowData);

    QList<BorrowedBook *> getReturnBooks() const;
    void setReturnBooks(const QList<BorrowedBook *> &newReturnBooks);


    void initWindow();
signals:
protected:
    void resizeEvent(QResizeEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;
private slots:
    void searchEvent();
    void restoreButtonEvent();
    void allSelectButtonEvent();
private:
    Ui::ReturnBook *ui;

    //借阅的图书集合
    QList<BorrowedBook*>borroweredBooks;
    //表格中展示的信息
    QList<BorrowedBook*>currentShowData;
    //借阅的图书文件路径
    QString borroweredBookFilePath;

    //已归还的图书文件路径
    QString returnedBookFilePath;
    //已勾选的图书集合
    QList<BorrowedBook*>returnBooks;

    //是否升序排列
    bool isAscendingOrder;
    //是否已全选
    bool isAllSelected;
    //ctrl+A全选快捷键
    //QShortcut *ctrlAShortCut;

    QFile*borroweredBookfile;
    QFile*returnedBookfile;

    QTextStream*borroweredBookStream;
    QTextStream*returnedBookStream;


    void initBooks();

    void setDataToTable(QString searchType, QString searchLine,bool isAscendingOrder);
    void setDataToTable(bool isAscendingOrder);

    void addEvents();

    void setCellWidgetSafely(int row, int column, const QWidget *widget);
    void setTableItem(int row, int column, const QString &text, Qt::Alignment alignment, int fontSize);
    QList<BorrowedBook*> sortData(const QList<BorrowedBook*> &data, bool isAscendingOrder);

    QString getCurrentCppDirectory();

};

#endif // RETURNBOOK_H
